﻿using ProductSystem.Model;
using Fundway.Common.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL.Common
{
    public class ServerAppConfigBll
    {
        private static string dicCacheKey = "ServerAppConfigDic";

        /// <summary>
        /// 添加/更新缓存
        /// </summary>
        public static void CacheDic()
        {
            try
            {
                using (EntitiesModel em = new EntitiesModel())
                {
                    Dictionary<string, string> dic = em.ServerAppConfigs.ToDictionary(t => t.Key, t => t.Value);
                    CacheRepository.AddOrReplace(dicCacheKey, dic, CacheExpirationTypes.Invariable);
                }
            }
            catch (Exception e)
            {
                //DOTO
            }
        }

        /// <summary>
        /// 检查缓存
        /// </summary>
        public static void CheckCacheDic()
        {
            try
            {
                if (CacheRepository.Get(dicCacheKey, true) == null)
                {
                    CacheDic();
                }
            }
            catch (Exception e)
            {
                //DOTO
            }
        }

        /// <summary>
        /// 从缓存中获取字典
        /// </summary>
        public static Dictionary<string, string> AppConfigDic
        {
            get
            {
                CheckCacheDic();
                Dictionary<string, string> result = new Dictionary<string, string>();
                try
                {
                    result = CacheRepository.Get(dicCacheKey, true) as Dictionary<string, string>;
                }
                catch (Exception e)
                {
                    //DOTO
                }
                return result;
            }
        }

        /// <summary>
        /// 判断数据库中是否已存在记录
        /// </summary>
        /// <param name="model">数据元</param>
        /// <returns></returns>
        public static bool IsExist(ServerAppConfig model)
        {
            bool flag = false;
            try
            {
                using (EntitiesModel em = new EntitiesModel())
                {
                    ServerAppConfig data = em.ServerAppConfigs.FirstOrDefault(t => t.Key == model.Key && t.Id != model.Id);
                    if (data != null) flag = true;
                }
            }
            catch (Exception e)
            {
                //DOTO
            }
            return flag;
        }

        /// <summary>
        /// 根据Id查询数据
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public static ServerAppConfig GetData(int id)
        {
            ServerAppConfig result;
            try
            {
                using (EntitiesModel em = new EntitiesModel())
                {
                    result = em.ServerAppConfigs.First(t => t.Id == id);
                }
            }
            catch (Exception e)
            {
                //DOTO
                result = new ServerAppConfig();
            }
            return result;
        }

        /// <summary>
        /// 根据关键字对Key和Value做模糊查询
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public static List<ServerAppConfig> GetList(string keyword, PageInfo pageInfo)
        {
            List<ServerAppConfig> result;
            try
            {
                using (EntitiesModel em = new EntitiesModel())
                {
                    result = em.ServerAppConfigs.Where(t => string.IsNullOrEmpty(keyword) || t.Key.Contains(keyword) || t.Value.Contains(keyword))
                        .OrderBy(o => o.Key).Page<ServerAppConfig>(pageInfo);
                }
            }
            catch (Exception e)
            {
                result = new List<ServerAppConfig>();
            }
            return result;
        }
    }
}
